﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTracker_BL.Repositories;
using Telerik.Web.Mvc;
using TimeTracker_Entities;
using TimeTracker.Models;

namespace TimeTracker.Controllers
{
    public class ProjectController : Controller
    {
        private ITimeTrackerRepository _db;
        private ILookupsRepository _lookups;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectController"/> class.
        /// </summary>
        public ProjectController()
        {
            _db = new TimeTrackerRepository();
            _lookups = new LookupsRepository();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectController"/> class.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="lookups">The lookups.</param>
        public ProjectController(ITimeTrackerRepository db, ILookupsRepository lookups)
        {
            _db = db;
            _lookups = lookups;
        }

        public ActionResult Index()
        {
            PopulateClientsLookup();

            return View();
        }

        /// <summary>
        /// Gets the clients from the database.
        /// </summary>
        [GridAction]
        public ActionResult _GetProjects()
        {
            // Get the list of clients from the database
            var clients = from p in _db.GetProjects()
                          orderby p.Client.ClientName, p.ProjectName
                          select new ProjectModel(p);

            return View(new GridModel<ProjectModel>(clients));
        }

        [GridAction]
        public ActionResult _AddProject(Project projectToAdd)
        {
            // Add the client to the database
            var result = _db.AddProject(projectToAdd);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetProjects();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        [GridAction]
        public ActionResult _UpdateProject(Project updatedProject)
        {
            // Update the client in the database
            var result = _db.UpdateProject(updatedProject);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetProjects();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        [GridAction]
        public ActionResult _DeleteProject(int deletedProjectId)
        {
            // Delete the client from the database
            var result = _db.DeleteProject(deletedProjectId);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetProjects();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        #region Private helpers
        /// <summary>
        /// Populates the clients lookup.
        /// </summary>
        private void PopulateClientsLookup()
        {
            ViewBag.Clients = _db.GetClients();
        }
        #endregion
    }
}
